package com.ppc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ppc.constant.MessageConstant;
import com.ppc.context.BaseContext;
import com.ppc.dto.EmployerRequireModifyDTO;
import com.ppc.dto.EmployerRequirePageQueryDTO;
import com.ppc.dto.EmployerRequirementDTO;
import com.ppc.entity.Requirement;
import com.ppc.exception.DeletionNotAllowedException;
import com.ppc.exception.SensitiveException;
import com.ppc.mapper.employer.EmployerMapper;
import com.ppc.mapper.employer.EmployerRequireMapper;
import com.ppc.result.PageResult;
import com.ppc.service.EmployerRequireService;
import com.ppc.utils.MatchServerUtil;
import com.ppc.vo.EmployerPageQueryVO;
import com.ppc.vo.EmployerRequireDetailVO;
import com.ppc.vo.EmployerTagVO;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 雇主需求实现
 *
 * @author LENOVO
 * @date 2023/12/20
 */
@Service
public class EmployerRequireServiceImpl implements EmployerRequireService {

  @Autowired
  private EmployerRequireMapper employerRequireMapper;

  @Autowired
  private EmployerMapper employerMapper;

  @Override
  /**
   * 创建需求
   * @param empRequirementDTO
   */
  public void createRequirement(EmployerRequirementDTO employerRequirementDTO) {
    if (employerRequirementDTO.checkSensitive()) {
      throw new SensitiveException(MessageConstant.CONTAINS_SENSITIVE + employerRequirementDTO.getSensitiveWord());
    }
    Requirement requirement = new Requirement();
    BeanUtils.copyProperties(employerRequirementDTO, requirement);
    Long userId = BaseContext.getCurrentId();
    Long employerId=employerMapper.getEmployerIdByUserId(userId);
    requirement.setRequireCreateTime(LocalDateTime.now());
    requirement.setRequireUpdateTime(LocalDateTime.now());
    requirement.setEmployerId(employerId);
    requirement.setRequireStatus(2);
    requirement.setRequirePeopleNum(1);
    employerRequireMapper.createRequirement(requirement);
    Long id = requirement.getRequireId();
    Requirement newRequirement = employerRequireMapper.getById(id);
    MatchServerUtil.addRequire(newRequirement.getRequireId(), newRequirement.getRequireTag());
  }

  @Override
  /**
   * 修改需求
   * @param requireModifyDTO
   */
  public void updateRequirement(EmployerRequireModifyDTO employerRequireModifyDTO) {
    if (employerRequireModifyDTO.checkSensitive()) {
      throw new SensitiveException(MessageConstant.CONTAINS_SENSITIVE+employerRequireModifyDTO.getSensitiveWord());
    }
    Requirement requirement=employerRequireMapper.getById(employerRequireModifyDTO.getRequireId());
    if (employerRequireModifyDTO.getRequireTag() == null||employerRequireModifyDTO.getRequireTag()==""
            || employerRequireModifyDTO.getRequireTag()==requirement.getRequireTag()) {
      employerRequireMapper.update(employerRequireModifyDTO);
    } else {
      employerRequireMapper.update(employerRequireModifyDTO);
      MatchServerUtil.changeRequire(employerRequireModifyDTO.getRequireId(), employerRequireModifyDTO.getRequireTag());
    }
  }

  /**
   * 查看需求详细信息
   *
   * @param requireId
   * @return
   */
  @Override
  public EmployerRequireDetailVO checkRequireDetail(Long requireId) {
    EmployerRequireDetailVO employerRequireDetailVO = employerRequireMapper.checkRequireDetail(
        requireId);
    return employerRequireDetailVO;
  }

  /**
   * 删除需求
   *
   * @param requireId,requireStatus
   */
  @Transactional
  public void requireDel(Long requireId) {
    try {
      Requirement requirement = employerRequireMapper.getById(requireId);
      int requireStatus=requirement.getRequireStatus();
      if (requireStatus == 3) {
        //当前需求已被接受，不能删除
        throw new DeletionNotAllowedException(MessageConstant.REQUIRE_STATUS_ERROR);
      } else {
        employerRequireMapper.requireDel(requirement.getRequireId());
        MatchServerUtil.deleteRequire(requireId);
      }
    } catch (Exception e) {
      throw new DeletionNotAllowedException("输入不正确，请重新输入");
    }
  }


  @Override
  /**
   * 分页查询
   * @param requirePageQueryDTO
   * @return
   */
  public PageResult pageQuery(EmployerRequirePageQueryDTO employerRequirePageQueryDTO) {
    Long userId=BaseContext.getCurrentId();
    Long employerId=employerRequireMapper.getEmployerIdByUserId(userId);
    PageHelper.startPage(employerRequirePageQueryDTO.getPage(),
        employerRequirePageQueryDTO.getPageSize());
    String requireStatus=employerRequirePageQueryDTO.getRequireStatus();
    String requireTitle=employerRequirePageQueryDTO.getRequireTitle();
    Page<EmployerPageQueryVO> page = employerRequireMapper.pageQuery(requireStatus,
        requireTitle,employerId);

    long total = page.getTotal();
    List<EmployerPageQueryVO> records = page.getResult();

    return new PageResult(total, records);

  }

  /**
   * 查看需求标签
   *
   * @return
   */
  public List<EmployerTagVO> checkTag() {
    List<EmployerTagVO> tag = employerRequireMapper.getRequireTag();
    return tag;
  }


}
